package com.moioo.exam;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.moioo.util.PageAuger;

/**
 * Data access object (DAO) for domain model class TVodClass.
 * @see com.moioo.exam.TVodClass
 * @author MyEclipse - Hibernate Tools
 */
public class TVodClassDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(TVodClassDAO.class);

	//property constants
	public static final String TITLE = "title";

    
    public void save(TVodClass transientInstance) {
        log.debug("saving TVodClass instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(TVodClass persistentInstance) {
        log.debug("deleting TVodClass instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public TVodClass findById( java.lang.Integer id) {
        log.debug("getting TVodClass instance with id: " + id);
        try {
            TVodClass instance = (TVodClass) getSession()
                    .get("com.moioo.exam.TVodClass", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(TVodClass instance) {
        log.debug("finding TVodClass instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.moioo.exam.TVodClass")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding TVodClass instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from TVodClass as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}
	
    public TVodClass merge(TVodClass detachedInstance) {
        log.debug("merging TVodClass instance");
        try {
            TVodClass result = (TVodClass) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(TVodClass instance) {
        log.debug("attaching dirty TVodClass instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(TVodClass instance) {
        log.debug("attaching clean TVodClass instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    public List findAll() {
    	log.debug("finding all TVodClass instances no where");
    	try {
			String queryString = " from TVodClass t order by t.id";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (HibernateException e) {
			log.error("find all on where failed", e);
			throw e;
		}
    }
    public List findAll(PageAuger pa) {
		log.debug("finding all TVodClass instances");
		try {
			String whereString = "";
			String queryString = "from TVodClass t ";
			if(pa.getStringWhereAuger("state",null) != null)
				whereString += " AND t.state = " + pa.getStringWhereAuger("state","2");
			if(pa.getStringWhereAuger("title",null) != null)
				whereString += " AND t.title like '%" + pa.getStringWhereAuger("title","0") + "%'";
			if(!"".equals(whereString))whereString = " WHERE " + whereString.substring(4);
			Query rows = getSession().createQuery("select count(t) from TVodClass as t " + whereString);
			pa.setRowCount(rows.uniqueResult());
			queryString = queryString + whereString + " order by t.id desc";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}